package com.lhl.fw.db.biz.service.impl;

import cn.hutool.json.JSONUtil;
import com.lhl.fw.db.biz.domain.entity.Person;
import com.lhl.fw.db.biz.repository.PersonRepository;
import com.lhl.fw.db.biz.service.PersonService;
import com.lhl.fw.db.service.BaseJpaServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @className: PersonServiceImpl
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-09-03 17:57
 **/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PersonServiceImpl extends BaseJpaServiceImpl<Person,String, PersonRepository> implements PersonService {

    //接受Person发出的类型为UserSaveEvent的DomainEvents事件
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
    public void event2(Person event){
        log.debug("TransactionPhase.BEFORE_COMMIT=event=={}", JSONUtil.toJsonStr(event));
    }

    /**
     * @param: null
     * @description:
     * BEFORE_COMMIT：事务提交之前，事务执行有异常失效
     * AFTER_COMMIT：事务成功提交之后，事务执行有异常失效(默认)
     * AFTER_ROLLBACK：事务回滚之后
     * AFTER_COMPLETION：事务完成之后，无论是否有异常都会执行
     * @return:
     * @author: king
     * @date: 2020-11-06
     */
    //接受Person发出的类型为UserSaveEvent的DomainEvents事件
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void event(Person event){
        log.debug("event=={}", JSONUtil.toJsonStr(event));
    }

    //接受Person发出的类型为UserSaveEvent的DomainEvents事件
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
    public void event3(Person event){
        log.debug("TransactionPhase.AFTER_COMPLETION=event=={}", JSONUtil.toJsonStr(event));
    }

    public void event4(){
        baseJpaRepository.findAll();
    }
}